<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class Label extends NotORM {

    /* 评价标签 */   	
    public function getEvaluate() {

		$list=\PhalApi\DI()->notorm->evaluate
				->select('id,name,name_en,colour,type')
                ->order('list_order asc')
				->fetchAll();  
        return $list; 
    }
	
    
	/* 更新主播统计 */
    public function upLiveEvaluate($liveuid,$type,$nums) {
        if($type==1){
            $rs=\PhalApi\DI()->notorm->user
					->where('id = ?', $liveuid)
					->update( array('goodnums' => new \NotORM_Literal("goodnums + {$nums}") ));
            \App\hIncrByFloat("userinfo_".$liveuid,'goodnums',$nums);
        }else{
            $rs=\PhalApi\DI()->notorm->user
					->where('id = ?', $liveuid)
					->update( array('badnums' => new \NotORM_Literal("badnums + {$nums}") ));
            \App\hIncrByFloat("userinfo_".$liveuid,'badnums',$nums);
        }
        
        /* 更新主播等级 */
        $info=\PhalApi\DI()->notorm->user
                    ->select('goodnums,badnums,level_anchor,voice_value,video_value,votestotal')
					->where('id = ?', $liveuid)
					->fetchOne();
		$level_anchor=\App\getLevelanchor($info['votestotal']);
        /* $level_anchor=getLevelanchor($info['goodnums'],$info['badnums']); */
        
        $data=[
            'level_anchor'=>$level_anchor,
        ];
        
        if($level_anchor<$info['level_anchor']){
            /* 降级 更新价格*/
            $fee_voice=\PhalApi\DI()->notorm->fee_voice
                    ->select('coin')
                    ->where('level <=?',$level_anchor)
                    ->order('coin desc')
                    ->fetchOne();

            $fee_video=\PhalApi\DI()->notorm->fee_video
                    ->select('coin')
                    ->where('level <=?',$level_anchor)
                    ->order('coin desc')
                    ->fetchOne();

            if($fee_voice && $info['voice_value'] > $fee_voice['coin'] ){
                $data['voice_value'] = $fee_voice['coin'];
            }
            
            if($fee_video && $info['video_value'] > $fee_video['coin'] ){
                $data['video_value'] = $fee_video['coin'];
            }    

        }

        \PhalApi\DI()->notorm->user
					->where('id = ?', $liveuid)
					->update( $data );
		return $rs;
    }

	/* 评价记录 */
    public function setEvaluate($data) {
        
        $rs=\PhalApi\DI()->notorm->evaluate_log
				->insert($data);
                
		return $rs;
    }
    
    /* 评价列表 */
    public function getEvaluateList($liveuid,$p) {
        
        if($p<1){
            $p=1;
        }
		$pnum=50;
		$start=($p-1)*$pnum;

        $list=\PhalApi\DI()->notorm->evaluate_log
				->select('*')
                ->where('liveuid=?',$liveuid)
                ->order('addtime desc')
                ->limit($start,$pnum)
                ->fetchAll();
				
		
                
		return $list;
    }

    
    /* 更新评价统计 */
    public function updateEvaluateCount($data) {
        
        $rs=\PhalApi\DI()->notorm->evaluate_count
                ->where('uid=? and name=?',$data['uid'],$data['name'])
                ->update( array('nums' => new \NotORM_Literal("nums + 1") ));
        if(!$rs){
           $rs= \PhalApi\DI()->notorm->evaluate_count
                ->insert($data);
        }
                
		return $rs;
    }
    
    /* 评价统计 */
    public function getEvaluateCount($liveuid) {

        $list=\PhalApi\DI()->notorm->evaluate_count
				->select('name,name_en,colour,nums')
                ->where('uid=?',$liveuid)
                ->order('type desc,nums desc')
                ->fetchAll();
                
		return $list;
    }

}
